package controllers;

import play.*;
import play.mvc.*;
import common.FBSecure;
import common.FBUtil;
import common.TVRageRequest;

import java.util.*;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import models.*;

@With(FBSecure.class)
public class Application extends FBController {

    public static void index() {
      
    	List<Serie> series = Serie.getSeriesOrderByRate(null);
    	
    	setFbIdandName();
    	
    	User user = getUser();
    	
    	List<Serie> follows = user.getFollows();
    	
    	
    	List<Video> videos = new ArrayList<Video>();
    	List<Topic> topics = new ArrayList<Topic>();
    	List<Episode> episodes = new ArrayList<Episode>();
    	List<CommentSerie> comments = new ArrayList<CommentSerie>();
    	
    	for (Serie s : follows) {
    		
    		if (s.getVideoList().size() > 0) {
    			videos.add(s.getVideoList().get(s.getVideoList().size()-1));
    		}
    		
    		if (s.getTopicList().size() > 0) {
    			topics.add(s.getTopicList().get(s.getTopicList().size()-1));
    		}
    		
    		if (s.getEpisodeList().size() > 0) {
    			episodes.add(s.getEpisodeList().get(s.getEpisodeList().size()-1));
    		}
    		
    		if (s.getCommentList().size() > 0) {
    			comments.add(s.getCommentList().get(s.getCommentList().size()-1));
    		}
    		
    		
    	}
    	
    	
    	List<Episode> todayEpisodes = Episode.upcomingEpisodes(new Date());
      
    	render(series, videos, topics, episodes, comments, todayEpisodes);
    }
    
    
	public static void welcome() {
        
    	
        setFbIdandName();
        
        
        User user = getUser();
        
        
        
        JsonElement je = FBUtil.getInstance().executeGraphRequest(getUser().getFacebookID() + "?fields=television.fields(name)", getOAuthToken());
        
        
        
        if ((je == null) || !je.isJsonObject()) {
	    	render();
        }
        ArrayList<String> seriesNames = new ArrayList<String>();
	    
	    if ((je.getAsJsonObject().has("television"))) {

	    	JsonElement data = je.getAsJsonObject().get("television").getAsJsonObject().get("data");
    		int numOfData = data.getAsJsonArray().size();
    		
    		
    		for(int i=0; i < numOfData; i++)
	    		seriesNames.add(data.getAsJsonArray().get(i).getAsJsonObject().get("name").getAsString());
	    }
        
        List<Serie> allSeries = Serie.findAll();
        ArrayList<Serie> series = new ArrayList<Serie>();
        
        
        
        for (String sName : seriesNames) {
        	
        	boolean found = false;
        	
        	for (Serie s : allSeries) {
        		
        		if (s.getName().equals(sName)) {
        			series.add(s);
        			found = true;
        			break;
        		}
        	}
        	
        	
        	if (!found) {
        		
        		
        		long tvrageid = TVRageRequest.getTVRageID(sName);
            	
            	
            	
            	for (Serie serie : allSeries) {
            		if (serie.getTvrageid() == tvrageid) {
            			series.add(serie);
            			found = true;
            			break;
            		}
            	}
            	
            	if (!found) {
            		Serie s = TVRageRequest.getSerie(tvrageid);
            		if (s != null)
            			series.add(s);
            	}
        		
        		
        	}
        	
        }
        
        
        render(series, user);
	}

}